#include "stagestep.h"
#include "ui_stagestep.h"

StageStep::StageStep(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StageStep)
{
    ui->setupUi(this);

    /* 取消扩增输入框右键菜单 */
    ui->raiseTempSpeedInput->setContextMenuPolicy(Qt::NoContextMenu);
    ui->finalTempInput->setContextMenuPolicy(Qt::NoContextMenu);
    ui->runningTimeHour->setContextMenuPolicy(Qt::NoContextMenu);
    ui->runningTimeMin->setContextMenuPolicy(Qt::NoContextMenu);

    /* 隐藏添加和删除按钮 */
    ui->actionButtons->hide();

    /* 限制合法输入内容 */
    ui->raiseTempSpeedInput->setValidator(new QDoubleValidator(0, 999999999999999999999999999.9, 1, this));
    ui->finalTempInput->setValidator(new QDoubleValidator(0, 999999999999999999999999999.9, 1, this));
    ui->runningTimeHour->setValidator(new QIntValidator(0, 1410065407, this));
    ui->runningTimeMin->setValidator(new QIntValidator(0, 1410065407, this));

    /* 将 data 组件从父元素的布局中除去 */
    ui->stageStepLayout->removeWidget(ui->dataCollection);
    ui->stageStepLayout->removeWidget(ui->finalTemp);
    ui->stageStepLayout->removeWidget(ui->raiseTempSpeed);
    ui->stageStepLayout->removeWidget(ui->runningTime);

    // 安装事件过滤器
    ui->holdStageFollower->installEventFilter(this);
    connect(ui->holdStageController, SIGNAL(valueChanged(int)), this, SLOT(updatePaint(int)));
}

StageStep::~StageStep()
{
    delete ui;
}

/* 扩增程序 */
void StageStep::paint(){
    int holdStageControllerHeight;
    QPainter painter(ui->holdStageFollower);
    QColor c(125,200,255);
    QPen pen(c, 1);


    holdStageControllerHeight = ui->holdStageController->geometry().height();

    painter.setPen(pen);
    painter.setBrush(c);

    QPointF point[4] = {QPointF(0, leftY), QPointF(0, holdStageControllerHeight), QPointF(95, holdStageControllerHeight), QPointF(95, rightY)};
    painter.drawPolygon(point, 4);
}

// 重写事件过滤器
bool StageStep::eventFilter(QObject *obj, QEvent *event){
    //如果在对象widget中有绘画的事件发生
    if(obj == ui->holdStageFollower && event->type() == QEvent::Paint)
    {
        this->paint();
    }
    //其他的交给父类去处理
    return QWidget::eventFilter(obj,event);
}

void StageStep::updatePaint(int i){
    int holdStageControllerHeight;
    double fixValue;

    holdStageControllerHeight = ui->holdStageController->geometry().height();

    /* 修正值：(当前slider的值 - slider最小值) x (滑块高度 / (slider最大值 - slider最小值)) */
    fixValue =(i - ui->holdStageController->minimum()) * SLIDER_HANDLER_HEIGHT / (ui->holdStageController->maximum() - ui->holdStageController->minimum());
    /* y轴偏移量：(当前slider的值 - slider最小值) x slider高度 / (slider最大值 - slider最小值) + 修正值 + 初始状态滑动条液面高度 */
    rightYDelta = (i - ui->holdStageController->minimum()) * holdStageControllerHeight/(ui->holdStageController->maximum() - ui->holdStageController->minimum()) - fixValue + SLIDER_INIT_HEIGHT;

    /* 计算每一个输入框数据的平均 y 坐标：滑动条高度，y轴偏移量，修正值 */
    int dataPosition = (int)(holdStageControllerHeight - rightYDelta + 28);

    ui->finalTempInput->setText(QString::number(i/10.0));

    ui->dataCollection->setGeometry(160, dataPosition, 24, 20);
    ui->finalTemp->setGeometry(110, dataPosition - 20, 68, 20);
    ui->raiseTempSpeed->setGeometry(0, dataPosition, 87, 20);
    ui->runningTime->setGeometry(10, dataPosition - 30, 91, 20);

    rightY = holdStageControllerHeight - rightYDelta;

    this->update();
}
void StageStep::leftUpdatePaint(int i){

    double fixValue;
    int holdStageControllerHeight;

    holdStageControllerHeight = ui->holdStageController->geometry().height();

    /* 修正值：(当前slider的值 - slider最小值) x (滑块高度 / (slider最大值 - slider最小值)) */
    fixValue =(i - ui->holdStageController->minimum()) * SLIDER_HANDLER_HEIGHT / (ui->holdStageController->maximum() - ui->holdStageController->minimum());
    /* y轴偏移量：(当前slider的值 - slider最小值) x slider高度 / (slider最大值 - slider最小值) + 修正值 + 初始状态滑动条液面高度 */
    rightYDelta = (i - ui->holdStageController->minimum()) * holdStageControllerHeight/(ui->holdStageController->maximum() - ui->holdStageController->minimum()) - fixValue + SLIDER_INIT_HEIGHT;


    leftY = holdStageControllerHeight - rightYDelta;

    this->update();
}

void StageStep::enterEvent(QEvent *event){
    ui->actionButtons->show();
}
void StageStep::leaveEvent(QEvent *event){
    ui->actionButtons->hide();
}
void StageStep::resizeEvent(QResizeEvent *event){
    this->updatePaint(ui->holdStageController->value());
}
void StageStep::showEvent(QShowEvent *event){
    emit StageStep::stepShow();
}
